/**
 * 
 */
package btree;

import java.io.IOException;

import global.EID;
import global.PID;
import global.PageId;
import heap.BasicPattern;
import heap.FieldNumberOutOfBoundException;
import heap.InvalidTupleSizeException;
import heap.InvalidTypeException;
import heap.Triple;

/**
 * @author smriti
 *
 */
public class KeyCombine {

	public String combine(Triple triple, int subject, int predicate, int object, int confidence){
		String finalKey = null;
		String[] arr = new String[4];
		try {
			if(subject != -1)
				arr[subject] = (subject == -1)?null:triple.getStrFld(1);
			if(predicate != -1)
				arr[predicate] = (predicate == -1)?null:triple.getStrFld(3);
			if(object != -1)
				arr[object] = (object == -1)?null:triple.getStrFld(5);
			if(confidence != -1)
				arr[confidence] = (confidence == -1)?null:triple.getStrFld(7);

			for (int i = 0; i < arr.length; i++) {
				if(null != arr[i]){
					if(null == finalKey){
						finalKey = arr[i];
						continue;
					}
					finalKey = finalKey + ":" + arr[i];
				}
			}
		} catch (FieldNumberOutOfBoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return finalKey;
	}

	public String combineStr(Triple triple, int subject, int predicate, int object, int confidence){
		String finalKey = null;
		String[] arr = new String[4];
		arr[0] = null;
		try {
			if(subject != -1)
				arr[subject] = (subject == -1)?null:triple.getSubjectID().pageNo.pid +","+triple.getSubjectID().slotNo;
			if(predicate != -1)
				arr[predicate] = (predicate == -1)?null:triple.getPredicateID().pageNo.pid + "," + triple.getPredicateID().slotNo;
			if(object != -1)
				arr[object] = (object == -1)?null:triple.getObjectID().pageNo.pid + "," + triple.getObjectID().slotNo;
			if(confidence != -1)
				arr[confidence] = (confidence == -1)?null:""+triple.getConfidence();

			for (int i = 0; i < arr.length; i++) {
				if(null != arr[i]){
					if(null == finalKey){
						finalKey = arr[i];
						continue;
					}
					finalKey = finalKey + ":" + arr[i];
				}
			}
		} catch (FieldNumberOutOfBoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return finalKey;
	}
	
	public String combineStrBP(BasicPattern BP) throws FieldNumberOutOfBoundException, IOException{
		String finalKey = BP.getIntFld(1) +","+ BP.getIntFld(2)+ ":" + BP.getIntFld(3)+","+ BP.getIntFld(4);
		return finalKey;
		
	}
	
	public String getStrKey(Triple triple, RdbIndexType rdfIndextype){
		String key = null;
		switch(rdfIndextype){
		case SPO : 
			key = this.combineStr(triple, 1, 2, 3, -1);
			break;
		case PSO :
			key = this.combineStr(triple, 2, 1, 3, -1);
			break;
		case SC : 
			key = this.combineStr(triple, 1, -1, -1, 2);
			break;
		case PC : 
			key = this.combineStr(triple, -1, 1, -1, 2);
			break;
		case OC : 
			key = this.combineStr(triple, -1, -1, 1, 2);
			break;
		case C : 
			key = this.combineStr(triple, -1, -1, -1, 1);
			break;	
		case SO:
			key = this.combineStr(triple, 1, -1, 2, -1);			
			break;
		case PO:
			key = this.combineStr(triple, -1, 1, 2, -1);			
			break;
		
		}
		
		return key;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		EID eid = new EID(new PageId(10),12);
		EID eid1 = new EID(new PageId(13),12);
//		EID eid2 = new EID(new PageId(15),12);
//		EID eid3 = new EID(new PageId(17),12);
//		EID eid4 = new EID(new PageId(19),12);
//		EID eid5 = new EID(new PageId(20),12);
//		EID eid6 = new EID(new PageId(30),12);
//
		PID pid = new PID(new PageId(54), 1);
//		PID pid2 = new PID(new PageId(4), 2);
//		PID pid3 = new PID(new PageId(5), 3);
//		PID pid4 = new PID(new PageId(13), 4);
//
		KeyCombine kc = new KeyCombine();
		Triple triple = null;
		try {
			triple = new Triple();
			triple.addTriple(eid, pid, eid1, new Float(0.7));
		} catch (InvalidTypeException e) {
			e.printStackTrace();
		} catch (InvalidTupleSizeException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (FieldNumberOutOfBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		kc.combine(triple, 2, -1, 1, 3);
//		System.out.println(kc.combine(eid6, new Float(0.9)));
//		System.out.println(kc.combine(eid5, new Float(0.9)));
//		System.out.println(kc.combine(eid3, new Float(0.9)));
//		System.out.println(kc.combine(pid, new Float(0.9)));
//		System.out.println(kc.combine(eid2,pid,eid));
//		System.out.println(kc.combine(eid6,pid4,eid1));
//		System.out.println(kc.combine(pid4,eid4,eid1));

	}

}
